package com.basics.api.controller;

import com.basics.api.request.user.AuthReq;
import com.basics.api.request.user.SaveNickNameReq;
import com.basics.api.request.user.UpdatePhoneReq;
import com.basics.api.response.LoginData;
import com.basics.api.response.user.UserPageInfoData;
import com.basics.api.service.UserApiService;
import com.basics.api.vo.UserInfo;
import com.basics.framework.web.domain.RestResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/user/")
@Api(tags = "用户模块")
public class UserApiController extends BaseApiController{

    @Autowired
    private UserApiService userApiService;

    @GetMapping("/getUserInfo")
    @ApiOperation("获取用户信息")
    public RestResult<UserInfo> getUserInfo(){
        return RestResult.success(userApiService.getUserInfoData(getCurrentUser().getUserId()));
    }

    @PostMapping("/saveUserBaseInfo")
    @ApiOperation("获取用户信息")
    public RestResult<String> saveUserBaseInfo(@RequestBody SaveNickNameReq req){
        userApiService.saveUserBaseInfo(req,getCurrentUser().getUserId());
        return RestResult.success();
    }

    @PostMapping("/authentication")
    @ApiOperation("实名认证")
    public RestResult<String> authentication(@RequestBody AuthReq req){
        userApiService.authentication(req,getCurrentUser().getUserId());
        return RestResult.success();
    }

    @ApiOperation(value = "原手机获取验证码")
    @PostMapping("/getOldPhoneCode")
    public RestResult<LoginData> getOldPhoneCode(@RequestParam(value = "phone")String phone){
        userApiService.getOldPhoneCode(phone,getCurrentUser().getUserId());
        return RestResult.success();
    }

    @ApiOperation(value = "更换手机号")
    @PostMapping("/updatePhone")
    public RestResult<String> updatePhone(@RequestBody UpdatePhoneReq req){
        userApiService.updatePhone(req,getCurrentUser().getUserId());
        return RestResult.success();
    }

    @GetMapping("/getUserPageInfo")
    @ApiOperation("获取用户主页信息(userId为空,查的是自己的信息)")
    public RestResult<UserPageInfoData> getUserPageInfo(@RequestParam(value = "userId",required = false)Long userId){
        if(userId==null){
            userId = getCurrentUser().getUserId();
        }
        return RestResult.success(userApiService.getUserPageInfo(userId
                ,getOrganizationId()));
    }

}
